#include "MapCreator.h"

#include <QHBoxLayout>


MapCreator::MapCreator(int width, int height, int gridSize, QWidget* parent)
	: QWidget( parent )
{
	QHBoxLayout* layout = new QHBoxLayout(this);
	this->setLayout(layout);

	m_toolbar = new Toolbar(this);
	layout->addWidget(m_toolbar);

	m_scene = new QGraphicsScene( QRectF(0, 0, gridSize*width, gridSize*height), this);
	m_view = new QGraphicsView(m_scene, this);

	layout->addWidget(m_view);

	m_grid.resize(width);
	for(int x = 0; x < width; x++)
		for(int y = 0; y < height; y++)
		{
			Tile* tile = new Tile(m_toolbar, gridSize, m_toolbar->currentPixmap(), m_toolbar->currentString());
			m_grid[x].push_back( tile );
			tile->setPos( tile->mapFromScene( QPointF(x*gridSize + gridSize/2, y*gridSize + gridSize/2)));
			m_scene->addItem(tile);
		}
	
}

MapCreator::~MapCreator()
{
	for(size_t x = 0; x < m_grid.size(); x++)
		for(size_t y = 0; y < m_grid[0].size(); y++)
			delete m_grid[x][y];

	m_grid.clear();
}

void MapCreator::setVakje(size_t x, size_t y, const QString& type )
{
	m_toolbar->select( type );
	m_grid[x][y]->doUpdate();
}

#include "MapParser.h"
#include "Map.h"


void MapCreator::saveMap( const QString& id )
{
	Map map;
	map.resize( m_grid.size(), m_grid[0].size());
	map.setNaam( id.toStdString() );
	for(size_t x = 0; x < map.width(); x++)
		for(size_t y = 0; y < map.height(); y++)
		{
			string type = m_grid[x][y]->getType().toStdString();
			cout << type << endl;
			map.setVakje(x, y, Vakje::create( type[0], type[1] ));
		}

	MapParser parser;
	parser.saveMap( map );
}
	

